#include <iostream>
#include <string>
#include <regex>

using namespace std;

int brute_match_v1(string text, string pattern)
{
    int n = text.length();
    int i = 0;

    int m = pattern.length();
    int j = 0;

    while (j < m && i <= n - m)
    {
        if (text[i] == pattern[j]) //匹配 检测下一个字符
        {
            i++;
            j++;
        }
        else //不匹配 则复位
        {
            i -= j - 1;
            j = 0;
        }
    }

    return i - j; //返回匹配的位置，由此判断是否匹配成功！！高明
}

int brute_match_v2(string text, string pattern)
{
    int n = text.length();
    int i = 0;

    int m = pattern.length();
    int j = 0;

    for (i; i < n; i++)
    {
        for (j = 0; j <= m; j++)
        {
            if (text[i + j] != pattern[j]) //j <= m  不会越界 最后一个字符'\0'
                break;
        }
        if (m == j)
            break;
    }
    return i;   //此处返回i 为匹配的位置
}


int main()
{
    string text = "asdfdddg";
    string pattern = "ddd";

    int f = brute_match_v2(text, pattern);
    cout << f << endl;
    //判断匹配位置 f = i-j; 最迟在n-m处,超过此处即是没有匹配成功*****
    (f <= text.length() - pattern.length()) ? cout << "true" << endl : cout << "false" << endl;

    return 0;
}